package com.ruoyi.eqmonitor.smdtv.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.eqmonitor.smdtv.domain.Smdtv10;
import com.ruoyi.eqmonitor.smdtv.mapper.Smdtv10Mapper;
import com.ruoyi.eqmonitor.smdtv.mapper.SmregisterMapper;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtv10Service;
import com.ruoyi.eqmonitor.utils.Constant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-09
 */
@Service
public class Smdtv10ServiceImpl extends ServiceImpl<Smdtv10Mapper, Smdtv10> implements ISmdtv10Service {

    @Resource
    private Smdtv10Mapper smdtv10Mapper;

    @Resource
    private SmregisterMapper smregisterMapper;

    @Override
    public Smdtv10 selectSmdtv10ById(Long id) {
        return smdtv10Mapper.selectSmdtv10ById(id,smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_10.getName()));
    }

    @Override
    public List<Smdtv10> getAll() {
        return smdtv10Mapper.selectSmdtv10List(new Smdtv10(),
                smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_10.getName()),
                null,null);
    }

    @Override
    public List<Smdtv10> selectSmdtv10List(Smdtv10 smdtv10,Integer pageNum,Integer pageSize) {
        return smdtv10Mapper.selectSmdtv10List(smdtv10,
                smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_10.getName()),
                pageNum,
                pageSize);
    }

    @Override
    public Long getTotal(Smdtv10 smdtv10) {
        return smdtv10Mapper.getTotal(smdtv10,
                smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_10.getName()));
    }

    @Override
    public Map<String, List<String>> treeMap() {
        //最外层
        Map<String, List<String>> map = new HashMap<>();
        List<Smdtv10> list = smdtv10Mapper.selectSmdtv10List(new Smdtv10(),
                smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_10.getName()),
                null,null);
        //根据所在水系分组
        if (list != null) {
            Map<String, List<Smdtv10>> collect2 = list.stream().collect(Collectors.groupingBy(Smdtv10::getRiverway));
            for (String key2 : collect2.keySet()) {
                //第2层
                List<String> stringList = new ArrayList<>();
                List<Smdtv10> smdtv10s2 = collect2.get(key2);
                if (smdtv10s2 != null) {
                    for (Smdtv10 smdtv10 : smdtv10s2) {
                        stringList.add(smdtv10.getName());
                    }
                    map.put(key2, stringList);
                }
            }
        }
        return map;
    }

    @Override
    public boolean save(Smdtv10 smdtv10) {
        return smdtv10Mapper.save(smdtv10,smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_10.getName())) > 0;
    }

    @Override
    public boolean updateById(Smdtv10 smdtv10) {
        return smdtv10Mapper.updateById(smdtv10,smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_10.getName())) > 0;
    }

    @Override
    public boolean removeByIds(List<Long> ids) {
        return smregisterMapper.removeByIds(ids,smregisterMapper.getTableNameByDatasetName(Constant.Smregister.SMDTV_10.getName())) > 0;
    }

}
